package com.yanqu.road.server.cmd.activity.throwingPot;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ThrowingPot;
import com.yanqu.road.entity.activity.args.ThrowingPotCombinationArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogThrowingPotLog;
import com.yanqu.road.pb.activity.ThrowingPotProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.throwingPot.ThrowingPotMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_THROWING_POT, desc = "投壶")
public class ThrowingPotCmd extends AbstractCommand {

    private int type;    // 0: 投一轮，1：投十轮
    private int activityId;

    private List<Integer> combinationIdList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ThrowingPotProto.ThrowingPotReqMsg reqMsg = ThrowingPotProto.ThrowingPotReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        Property consume = new Property();
        Property reward = new Property();
        BigInteger conditionAddValue = BigInteger.valueOf(0);
        int times;
        ActivityInfo activityInfo = ThrowingPotMgr.getActivityInfo();
        if (activityInfo == null || activityInfo.getActivityId() != activityId) {
            ret = GameErrorCode.E_THROWING_POT_ACTIVITY_NO_IN_TIME;
            return;
        } else if (!ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_THROWING_POT_ACTIVITY_NO_IN_TIME;
            return;
        }
        if (0 == type) {
            consume.addProperty(eItemId.THROWING_POT_JIAN_DAI.getValue(), BigInteger.valueOf(3));
            times = 1;
        } else if (1 == type) {
            consume.addProperty(eItemId.THROWING_POT_JIAN_DAI.getValue(), BigInteger.valueOf(27));
            times = 10;
        } else {
            ret = GameErrorCode.E_THROWING_POT_ARGUMENT_ERROR;
            return;
        }

        if (!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.ThrowingPot, eLogMoneyType.ThrowingPotThrowingPot)) {
            ret = GameErrorCode.E_THROWING_POT_ITEM_NO_ENOUGH;
            return;
        }

        Map<Integer, ThrowingPot> throwingPotMap = ThrowingPotMgr.getThrowingPotMap();

        int sumWeight = 0;
        for (ThrowingPot throwingPot : throwingPotMap.values()) {
            sumWeight += throwingPot.getWeight();
        }
        RandomHelper randomHelper = new RandomHelper();
        for (int i = 0; i < times; i++) {
            int randomValue = randomHelper.next(0, sumWeight);
            int tempWeight = 0;
            for (ThrowingPot throwingPot : throwingPotMap.values()) {
                tempWeight += throwingPot.getWeight();
                if (tempWeight > randomValue) {
                    combinationIdList.add(throwingPot.getCombinationId());
                    break;
                }
            }
        }

        for (int combinationId : combinationIdList) {
            ThrowingPot throwingPot = throwingPotMap.get(combinationId);
            List<String> rewardStrList = StringUtils.stringToStringList(throwingPot.getReward(), ";");
            for (String rewardStr : rewardStrList) {
                List<String> strings = StringUtils.stringToStringList(rewardStr, "=");
                int goodsId = Integer.valueOf(strings.get(0));
                BigInteger num = new BigInteger(strings.get(1));
                reward.addProperty(goodsId, num);
                if (goodsId == eItemId.THROWING_POT_SCORE.getValue()) {
                    conditionAddValue = conditionAddValue.add(num);
                }
            }
            //投壶组合次数条件
            player.notifyListener(eGamePlayerEventType.ThrowingPotCombinationTimes.getValue(), new ThrowingPotCombinationArgs(combinationId, 1));
            //记录日志
            AutoLogMgr.add(new LogThrowingPotLog(player.getUserId(), activityId, combinationId, throwingPot.getReward(), System.currentTimeMillis()));
        }

        //奖励入袋
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.ThrowingPot, eLogMoneyType.ThrowingPotThrowingPot);
        //投壶本服排行榜
        player.notifyListener(eGamePlayerEventType.ThrowingPotScoreRank.getValue(), conditionAddValue);
        //投壶跨服排行榜
        player.notifyListener(eGamePlayerEventType.ThrowingPotScoreCrossRank.getValue(), conditionAddValue);
    }

    @Override
    public void sendData(GamePlayer player) {
        ThrowingPotProto.ThrowingPotRespMsg.Builder respMsg = ThrowingPotProto.ThrowingPotRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.addAllCombinationIdList(combinationIdList);
        player.sendPacket(Protocol.U_THROWING_POT, respMsg);
    }
}